package com.xigua.xcar.shopdetail;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xigua.xcar.R;
import com.xigua.xcar.RoutePlan;
import com.xigua.xcar.base.BaseActivity;
import com.xigua.xcar.constant.Constants;
import com.xigua.xcar.data.CommonListData;
import com.xigua.xcar.data.ShopInfoData;
import com.xigua.xcar.helper.CalDistance;
import com.xigua.xcar.net.AbstractDataManager;
import com.xigua.xcar.net.getshopdetail.GetShopDetailData;
import com.xigua.xcar.net.getshopdetail.GetShopDetailDataManager;
import com.xigua.xcar.util.CommonListUtil;
import com.xigua.xcar.widgets.CommonList.CommonListAdapter;
import com.xigua.xcar.widgets.ShopInfo.ShopInfoAdapter;

public class ShopDetailActivity extends BaseActivity {
	private static final String tag = "ShopDetailActivity";
	private ListView mShopInfoListView = null;
	private ShopInfoAdapter mShopInfoAdapter = null;
	private ArrayList<ShopInfoData> mShopInfoArray;
	private ListView mServiceListView = null;
	private CommonListAdapter mServiceAdapter = null;
	private ArrayList<CommonListData> mServiceInfoArray = null;
	private ListView mSaleInfoListView = null;
	private CommonListAdapter mSaleInfoAdapter = null;
	private ArrayList<CommonListData> mSaleInfoArray = null;
	private TextView mTxtShopName = null;
	private TextView mTxtDesc = null;
	private TextView mTxtDistance = null;
	private ProgressDialog mProgressDialog = null; // 加载对话框
	private GetShopDetailDataManager mDM = null;
	private boolean isGettingData = false;
	private ProgressBar mLoadingBar = null;
	private RelativeLayout mMain = null;
	@Override
	protected void finalize() throws Throwable {
		// 注销的注意事项
		mDM.setHanlder(null);
		mDM.cancelRequest();
		mDM = null;
		super.finalize();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(tag, "onCreate");
		super.onCreate(savedInstanceState);
		setView(R.layout.shopdetail);
		setTopBarTitle("详情");
		setTitleBar(R.drawable.title_button, "返回", 0, "");
		mLoadingBar = (ProgressBar)findViewById(R.id.progressbar_shopdetail_loading);
		mMain = (RelativeLayout)findViewById(R.id.layout_shopdetail_main);
		mShopInfoArray = new ArrayList<ShopInfoData>();
		mServiceInfoArray = new ArrayList<CommonListData>();
		mSaleInfoArray = new ArrayList<CommonListData>();
		mTxtShopName = (TextView) findViewById(R.id.txt_shopdetail_shopName);
		mTxtDesc = (TextView) findViewById(R.id.txt_shopdetail_saleInfo);
		mTxtDistance = (TextView) findViewById(R.id.txt_shopdetail_distance);
		mShopInfoListView = (ListView) findViewById(R.id.listview_shopdetail_contact);
		mServiceListView = (ListView) findViewById(R.id.listview_shopdetail_servicelistview);
		mSaleInfoListView = (ListView) findViewById(R.id.listview_shopdetail_salelistview);
		mShopInfoAdapter = new ShopInfoAdapter(this);
		mServiceAdapter = new CommonListAdapter(this);
		mSaleInfoAdapter = new CommonListAdapter(this);
		mShopInfoAdapter.setData(mShopInfoArray);
		mShopInfoListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int index,
					long arg3) {
				if(0 == mShopInfoArray.size()){
					Toast.makeText(ShopDetailActivity.this, "参数不合法", 3000).show();
					return ;
				}
				index = (index%(mShopInfoArray.size()));
				ShopInfoData data = mShopInfoArray.get(index);
				if(0 == data.getType()){
					if(null != data.getmContent()){
						CommonListUtil.getPopupWindow(ShopDetailActivity.this, arg1, data.getmContent(), mTxtShopName.getText().toString());
					}else{
						Toast.makeText(ShopDetailActivity.this, "参数不合法", 3000).show();
					}
				}else if(1 == data.getType()){
					if(0 == data.getLongitude() || 0 == data.getLongitude()){
						Toast.makeText(ShopDetailActivity.this, "参数不合法", 3000).show();
					}else{
						Intent intent = new Intent(ShopDetailActivity.this,RoutePlan.class);
						intent.putExtra(Constants.Longitude, data.getLongitude());
						intent.putExtra(Constants.Latitude, data.getLatitude());
						ShopDetailActivity.this.startActivity(intent);
					}
				}
			}
		});
		mServiceAdapter.setData(mServiceInfoArray);
		mSaleInfoAdapter.setData(mSaleInfoArray);
		mShopInfoListView.setAdapter(mShopInfoAdapter);
		mServiceListView.setAdapter(mServiceAdapter);
		mSaleInfoListView.setAdapter(mSaleInfoAdapter);
//		ImageView mShopIcon = (ImageView) findViewById(R.id.image_shopdetail_shopPhoto);
		mDM = new GetShopDetailDataManager(this);
		mDM.setHanlder(new Handler(){

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if (mProgressDialog != null) {
					mProgressDialog.dismiss();
					mProgressDialog = null;
				}
				switch(msg.what){
				case AbstractDataManager.RESULTCODE_SUCCESS:{
					GetShopDetailData data = (GetShopDetailData)msg.obj;
					if(null != data){
						ArrayList<ShopInfoData> shopInfoArray = data.getArray();
						if(null!= shopInfoArray){
							mShopInfoArray.clear();
							for(int i=0;i<shopInfoArray.size();i++){
								mShopInfoArray.add(shopInfoArray.get(i));
							}
						}
						ArrayList<CommonListData> serviceInfoArray = data.getServiceInfoArray();
						if(null != serviceInfoArray){
							mServiceInfoArray.clear();
							for(int i=0;i<serviceInfoArray.size();i++){
								mServiceInfoArray.add(serviceInfoArray.get(i));
							}
						}
						ArrayList<CommonListData> saleInfoArray = data.getSaleInfoArray();
						if(null != saleInfoArray){
							mSaleInfoArray.clear();
							for(int i=0;i<saleInfoArray.size();i++){
								mSaleInfoArray.add(saleInfoArray.get(i));
							}
						}
//						if(null == data.getUrl() || "".equals(data.getUrl())){
//							mShopIcon.setBackgroundResource(R.drawable.default_shop);
//						}
						String distance = null;
						if((0 == CommonListUtil.getLocationPointFromSP(ShopDetailActivity.this).getLatitude() || 0 == CommonListUtil.getLocationPointFromSP(ShopDetailActivity.this).getLontitude())){
//							Double temp = CalDistance.gps2m(Double.parseDouble(data.getLatitude()+""), Double.parseDouble(data.getLongtitude()+""), 0, 0);
							distance = "";
						}else{
							Double temp = CalDistance.gps2m(Double.parseDouble(data.getLatitude()+""), Double.parseDouble(data.getLongtitude()+""),
									CommonListUtil.getLocationPointFromSP(ShopDetailActivity.this).getLatitude(), CommonListUtil.getLocationPointFromSP(ShopDetailActivity.this).getLontitude());
							distance = "" + temp.intValue();
						}
						mTxtShopName.setText(data.getShopName());
						mTxtDesc.setText(data.getDesc());
						Log.i(tag, "distance:" + distance);
						try{
							int realDistance = Integer.parseInt(distance);
							String strDistance = null;
							if(realDistance>=1000){
								strDistance = (realDistance/1000) + "." + ((realDistance%1000)/100) + "千米";
							}else{
								strDistance = realDistance + "米";
							}
							mTxtDistance.setText(strDistance);
						}catch(Exception e){
							mTxtDistance.setText("----");
						}
						mShopInfoAdapter.notifyDataSetChanged();
						mSaleInfoAdapter.notifyDataSetChanged();
						mServiceAdapter.notifyDataSetChanged();
						mLoadingBar.setVisibility(View.GONE);
						mMain.setVisibility(View.VISIBLE);
					}else{
						Toast.makeText(ShopDetailActivity.this, "网络异常", 3000).show();
						ShopDetailActivity.this.finish();
					}
//					if(null != data){
//						ArrayList<ShopListData> datas = data.getArray();
//						for(int i=0;i<datas.size();i++){
//							mArray.add(datas.get(i));
//						}
//						mAdapter.notifyDataSetChanged();
//					} 
					break;
				}
				case AbstractDataManager.RESULTCODE_NETERR:
				case AbstractDataManager.RESULTCODE_OTHERERR:{
					Toast.makeText(ShopDetailActivity.this, "网络异常", 3000).show();
					ShopDetailActivity.this.finish();
					break;
				}
				}
			}
			
		});
		Intent intent = getIntent();
		int shopId = intent.getIntExtra(Constants.SHOPID, 0);
		if(0 == shopId){
			Toast.makeText(this, "参数不合法，请重试", 3000).show();
			this.finish();
		}else{
			mDM.setShopId(shopId);
		}
		initData();
	}

	/**
	 * 显示加载进度条
	 * */
	private void showProgressDlg() {
		// HomePageActivityGroup parent = (HomePageActivityGroup)getParent();
		mLoadingBar.setVisibility(View.VISIBLE);
		mMain.setVisibility(View.GONE);
//		if (mProgressDialog == null) {
//			mProgressDialog = new ProgressDialog(this);
//			// 设置样式
//			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//			// 设置ProgressDialog 提示信息
//			mProgressDialog.setMessage("加载中");
//			// 设置ProgressDialog 的进度条是否不明确
//			mProgressDialog.setIndeterminate(false);
//			// 设置ProgressDialog 是否可以按退回按键取消
//			mProgressDialog.setCancelable(true);
//			// 让ProgressDialog显示
//			mProgressDialog.show();
//			mProgressDialog.setOnDismissListener(new OnDismissListener() {
//
//				@Override
//				public void onDismiss(DialogInterface dialog) {
//					// TODO Auto-generated method stub
//					Log.i(tag, "onDismiss");
//					cancelRequest();
//					mProgressDialog = null;
//				}
//			});
//		}
	}

	void initData(){
		ShopInfoData shopInfoData = new ShopInfoData();
		shopInfoData.setLatitude(0);
		shopInfoData.setLongitude(0);
		shopInfoData.setmContent("让数据飞一会");
		shopInfoData.setmResId(R.drawable.icon_phone);
		shopInfoData.setType(0);
		mShopInfoArray.add(shopInfoData);
		shopInfoData = new ShopInfoData();
		shopInfoData.setLatitude(0);
		shopInfoData.setLongitude(0);
		shopInfoData.setmContent("让数据飞一会");
		shopInfoData.setmResId(R.drawable.icon_address);
		shopInfoData.setType(1);
		mShopInfoArray.add(shopInfoData);
		CommonListData serviceInfo = new CommonListData();
		serviceInfo.setmPhoneNum("");
		serviceInfo.setmResId(R.drawable.icon_rightarray);
		serviceInfo.setmShopName("让数据飞一会");
		mServiceInfoArray.add(serviceInfo);
		serviceInfo = new CommonListData();
		serviceInfo.setmPhoneNum("");
		serviceInfo.setmResId(R.drawable.icon_rightarray);
		serviceInfo.setmShopName("让数据飞一会");
		mServiceInfoArray.add(serviceInfo);
		CommonListData saleInfo = new CommonListData();
		saleInfo.setmPhoneNum("");
		saleInfo.setmResId(R.drawable.icon_rightarray);
		saleInfo.setmShopName("让数据飞一会");
		mSaleInfoArray.add(saleInfo);
		saleInfo = new CommonListData();
		saleInfo.setmPhoneNum("");
		saleInfo.setmResId(R.drawable.icon_rightarray);
		saleInfo.setmShopName("让数据飞一会");
		mSaleInfoArray.add(saleInfo);
		mTxtShopName.setText("让数据飞一会");
		mTxtDistance.setText("----");
		mTxtDesc.setText("让数据飞一会");
		mShopInfoAdapter.notifyDataSetChanged();
		mSaleInfoAdapter.notifyDataSetChanged();
		mServiceAdapter.notifyDataSetChanged();
	}
	
	private void sendRequest() {
		if(!isGettingData){
			isGettingData = true;
			mDM.getDataFromServer();
		}
	}

	private void cancelRequest() {
		if(isGettingData){
			mDM.cancelRequest();
			isGettingData = false;
		}
	}

	@Override
	protected void handleTitleBarEvent(int buttonId) {
		switch (buttonId) {
		case Constants.LEFTBUTTONID: {
			this.finish();
			break;
		}
		case Constants.RIGHTBUTTONID: {
			break;
		}
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.i(tag, "onPause");
		cancelRequest();
		if(null != mProgressDialog){
			mProgressDialog.dismiss();
			mProgressDialog = null;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.i(tag, "onResume");
		if(null == mDM.getData()){
			showProgressDlg();
			sendRequest();
		}else{
			mLoadingBar.setVisibility(View.GONE);
			mMain.setVisibility(View.VISIBLE);
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		Log.i(tag, "onNewIntent");
		super.onNewIntent(intent);
	}

	@Override
	protected void onRestart() {
		Log.i(tag, "onRestart");
		super.onRestart();
	}

	@Override
	protected void onStart() {
		Log.i(tag, "onStart");
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.i(tag, "onStop");
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		Log.i(tag, "onDestroy");
		super.onDestroy();
//		unregisterReceiver(mBroadcastReceiver);
	}
}
